package com.study.interceptor;

import com.study.utils.ExecutorPluginUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.Properties;

//拦截Executor.query(MappedStatement var1, Object var2, RowBounds var3, ResultHandler var4) 方法
@Intercepts({
        @Signature(
                type= Executor.class,
                method = "query",
                args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
        )
})
@Slf4j
public class ExecutorInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {  //方法是用来对拦截的sql进行具体的操作。
        String sql = ExecutorPluginUtils.getSqlByInvocation(invocation);
        //可以对sql重写
        log.info("拦截器ExecutorInterceptor:"+sql);
        //sql = "SELECT id from BUS_RECEIVER where id = ? ";  //改写sql, 达到拦截效果
        ExecutorPluginUtils.resetSql2Invocation( invocation,  sql);
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object o) {   ///指定哪些方法可以被此拦截器拦截。
        return Plugin.wrap(o, this);
    }

    @Override
    public void setProperties(Properties properties) {  //方法主要是用来从配置中获取属性。

    }


}